package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 购物车的控制器
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 添加购物车
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/add")
    public Result add(@RequestParam("skuId") String skuId , @RequestParam("num") Integer num){
        //查询出当前用户的唯一标识,username
//        String username = "itcast";//当前商品是谁购买的,当前登录的用户
        //通过OAuth2AuthenticationDetails获取jwt令牌
        //解析jwt令牌,并转换成map集合,获取到对应的原始数据,其中就包含用户名称.登录的用户名称.
        String username = tokenDecode.getUserInfo().get("username");
        cartService.add(skuId,num,username);

        return new Result(true, StatusCode.OK,"添加购物车成功");
    }

    /**
     * 获取购物车列表
     * @return
     */
    @GetMapping("/list")
    public Map list(){
        //查询出当前用户的唯一标识,username
//        String username = "itcast";//当前商品是谁购买的,当前登录的用户
        String username = tokenDecode.getUserInfo().get("username");
        Map<String,Object> resultMap = cartService.list(username);
        return resultMap;
    }

}
